package com.andy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.andy.service.ITeacherService;
import com.andy.vo.CommonMessageResultVO;
import com.andy.vo.ExamSubjectVO;

@Controller
@RequestMapping(value = "/teacher")
public class TeacherController {
	
	@Resource(name = "teacherService")
	private ITeacherService teacherService;
	
	@RequestMapping(value = "/toManageSubjects")
	public String toManageSubjects(HttpSession session, Model model) {
		List<ExamSubjectVO> exmaSubjectList = teacherService.getAllSubjects();
		model.addAttribute("exmaSubjectList", exmaSubjectList);
		return "teacher/manageSubjects";
	}
	
	/*
	 * 
	 * 后台就这样
	 */
	@ResponseBody
	@RequestMapping(value = "/addSubject")
	public CommonMessageResultVO addSubject(@Valid ExamSubjectVO paramVO, BindingResult result) {	// 注解后台校验
		// 返回后台校验的结果
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		resultVO.setSuccess(false);
		
		// 判断JSR303是否校验有不通过的
		if (result.hasErrors()) {
			List<ObjectError> errorList = result.getAllErrors();
			Map<String, String> messageMap = new HashMap<String, String>();
			// 记录所有不通过的字段
			for (ObjectError error : errorList) {
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
//		boolean isSuccess = false;
		try {
			teacherService.addSubject(paramVO);
			resultVO.setSuccess(true);
//			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/editSubject")
	public boolean editSubject(ExamSubjectVO paramVO) {
		boolean isSuccess = false;
		try {
			teacherService.editSubject(paramVO);
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSuccess;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubject/{subId}")
	public ExamSubjectVO getSubject(@PathVariable Integer subId) {
		ExamSubjectVO resultVO = teacherService.getSubjectById(subId);
		return resultVO;
	}

	@ResponseBody
	@RequestMapping(value = "/delSubject")
	public boolean delSubject(Integer subId) {
		boolean isSuccess = false;
		try {
			teacherService.delSubject(subId);
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSuccess;
	}
	
	@ResponseBody
	@RequestMapping(value = "/changeSubjectStatus")
	public boolean changeSubjectStatus(Integer subId, Integer status) {
		boolean isSuccess = false;
		try {
			teacherService.changeSubjectStatus(subId, status);
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSuccess;
	}
	
	@RequestMapping(value = "/testAddSubject", method = RequestMethod.GET)
	public String toAdd() {
		return "teacher/add";
	}
	
	@RequestMapping(value = "/testAddSubject", method = RequestMethod.POST)
	public void doAdd(ExamSubjectVO vo, String[] status) {
		System.out.println("=========================================" + status);
		System.out.println(vo);
	}
	
	@ResponseBody
	@RequestMapping(value = "/subjectValidate")
	public boolean subjectValidate(String description) {
		if (description != null && description.contains("abc")) {	
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获取所有当前科目关联的学生的后台方法
	 * @param subjectId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllStudent")
	public Map<String, Object> getAllStudent(Integer subjectId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("allUser", teacherService.getAllStudent());//获取所有学生，包括没关联的
		resultMap.put("selectedUser", teacherService.getAllUserBySubject(subjectId));	// 获取所有已关联的学生
		return resultMap;
	}

	/**
	 * 保存修改
	 * @param subjectStudents
	 * @param subjectId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/saveSubjectStudents")
	public Map<String, Object> saveSubjectStudents(@RequestParam(name = "subjectStudents[]")String[] subjectStudents, Integer subjectId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", false);
		// subjectStudents  -> list1
		
		// String[] 转化成List<String>
		List<String> list = new ArrayList<String>();
		for (String student : subjectStudents) {
			list.add(student);
		}
		// teacherService.getAllUserBySubject(subjectId) --> list2
		try {
			// 传到Service,由Service来处理
			teacherService.saveSubjectStudent(list, subjectId);
			resultMap.put("success", true);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

}
